package com.zff.wechat.despatcher.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zff.web.constants.ButtonConstants;
import com.zff.web.constants.Constants;
import com.zff.web.dao.ConstantsMapper;
import com.zff.web.dao.WxUserLocationMapper;
import com.zff.web.outsideInter.GaoDeMapService;
import com.zff.web.po.ConstantsPo;
import com.zff.web.po.WxUserLocationPo;
import com.zff.wechat.despatcher.EventDispatcherService;
import com.zff.wechat.message.resp.TextMessage;
import com.zff.wechat.util.MessageUtil;


/**
 * @ClassName:  EventDispatcher   
 * @Description:TODO(事件消息业务分发器)   
 * @author: Android_Robot  
 * @date:   2017年6月2日 上午11:34:31   
 *
 */
@Service
public class EventDispatcherImpl implements EventDispatcherService{
	
	private static final Log LOG = LogFactory.getLog(EventDispatcherImpl.class);
	
	private static SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd ");
	
	@Autowired
	private ConstantsMapper constantsMapper;
	
	@Autowired
	private WxUserLocationMapper wxUserLocationMapper;
	
	@Autowired
	private GaoDeMapService gaoDeMapService;
	
	public String processEvent(Map<String, String> map) {
		String openid=map.get("FromUserName"); //用户 openid
    	String mpid=map.get("ToUserName");   //公众号原始 ID
        if (map.get("Event").equals(MessageUtil.EVENT_TYPE_SUBSCRIBE)) { //关注事件
            LOG.info("==============这是关注事件！");
            TextMessage txtmsg=new TextMessage();
        	txtmsg.setFromUserName(mpid);
        	txtmsg.setToUserName(openid);
        	txtmsg.setCreateTime(new Date().getTime());
        	txtmsg.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_TEXT);
        	if (map.get("MsgType").equals(MessageUtil.REQ_MESSAGE_TYPE_EVENT)) { // 文本消息
        		ConstantsPo constants = constantsMapper.fetcheValueByCode(Constants.CONSTANTS_CODE);
        	    txtmsg.setContent(constants.getValue());
        	    return MessageUtil.textMessageToXml(txtmsg);
        	}
        }

        if (map.get("Event").equals(MessageUtil.EVENT_TYPE_UNSUBSCRIBE)) { //取消关注事件
            LOG.info("==============这是取消关注事件！");
        }

        if (map.get("Event").equals(MessageUtil.EVENT_TYPE_SCAN)) { //扫描二维码事件
            LOG.info("==============这是扫描二维码事件！");
        }

        if (map.get("Event").equals(MessageUtil.EVENT_TYPE_LOCATION)) { //位置上报事件
            LOG.info("==============这是位置上报事件！");
            WxUserLocationPo wxUserLocationPo = new WxUserLocationPo();
            String time = map.get("CreateTime");
            long createTime = 0;
			try {
				createTime = Long.parseLong(time);
			} catch (NumberFormatException e) {
				LOG.info("createTime转换有误"+e);
				e.printStackTrace();
			}
            wxUserLocationPo.setFromUserName(openid);
            wxUserLocationPo.setToUserName(mpid);
            wxUserLocationPo.setCreateTime(createTime);
            wxUserLocationPo.setLatitude(map.get("Latitude"));
            wxUserLocationPo.setLongitude(map.get("Longitude"));
            wxUserLocationPo.setAccuracy(map.get("Precision"));;
            wxUserLocationPo.setEvent(MessageUtil.EVENT_TYPE_LOCATION);
            wxUserLocationPo.setMsgType(MessageUtil.REQ_MESSAGE_TYPE_EVENT);
            wxUserLocationMapper.insert(wxUserLocationPo);
        }

        if (map.get("Event").equals(MessageUtil.EVENT_TYPE_CLICK)) { //自定义菜单点击事件
        	// 普通文本消息
         	TextMessage txtmsg=new TextMessage();
         	txtmsg.setFromUserName(mpid);
         	txtmsg.setToUserName(openid);
         	txtmsg.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_TEXT);
            LOG.info("==============这是自定义菜单点击事件！");
            // 人脸识别
            // {CreateTime=1499413562, EventKey=facerecognition, Event=CLICK, ToUserName=gh_68b3159f270a, FromUserName=o-YEj1Fkx0pmR7gcUtI2GE2naURk, MsgType=event}
            String EventKey = map.get("EventKey");
            if (ButtonConstants.FACE_RECOGNITION.equals(EventKey)) {
            	//返回文本消息
            	txtmsg.setCreateTime(new Date().getTime());
            	txtmsg.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_TEXT);
            	txtmsg.setContent("请直接将您的照片发送给狗蛋");
            	return MessageUtil.textMessageToXml(txtmsg);
			}
            if (ButtonConstants.MY_LOCATION.equals(EventKey)) {
            	// 根据用户名获取用户Location信息
        		WxUserLocationPo userLocationPo = wxUserLocationMapper.fetchUserLocationByFromUser(openid);
        		// 将获取到的坐标信息传入高德地图定位方法中
        		String userLocation = gaoDeMapService.userLocation(userLocationPo.getLatitude(), userLocationPo.getLongitude());
//        		String userLocation = gaoDeMapService.userLocation("31.204782", "121.615869");
        		txtmsg.setCreateTime(new Date().getTime());
        		txtmsg.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_TEXT);
        		if (map.get("MsgType").equals(MessageUtil.REQ_MESSAGE_TYPE_EVENT)) {
                	txtmsg.setContent(userLocation);
                	return MessageUtil.textMessageToXml(txtmsg);
        		}
			}
        }

        if (map.get("Event").equals(MessageUtil.EVENT_TYPE_VIEW)) { //自定义菜单 View 事件
            LOG.info("==============这是自定义菜单 View 事件！");
        }

        return null;
    }
}
